package bit_0522_0311;

public class LinkedList {
    class Node {
        public int data;
        public Node next;
        public Node(int data) {
            this.data = data;
        }
    }
    public Node head;//头节点
    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if (index < 0 || index > size()) {
            System.out.println("超出范围");
            return false;
        }
        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == size()) {
            addLast(data);
            return true;
        }
        Node cur = head;
        Node node = new Node(data);
        while (index > 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (head == null) return;
        Node nhead = new Node(-1);
        Node cur = nhead;
        cur.next = head;
        while (cur.next != null) {
            if (cur.next.data == key) {
                cur.next = cur.next.next;
                head = nhead.next;
                return;
            }
            cur = cur.next;
        }

        System.out.println("没有要删掉的数据");
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
//        if (head == null) {
//            System.out.println("表头为空");
//            return;
//        }
//        Node nhead = new Node(-1);
//        Node cur = nhead;
//        cur.next = head;
//        while (cur.next != null) {
//            if (cur.next.data == key) {
//                cur.next = cur.next.next;
//            }
//            else {
//                cur = cur.next;
//            }
//        }
//        head = nhead.next;
        if (head == null) return;
        Node prev = head;
        Node cur = head.next;
        while (cur != null) {
            if (cur.data == key) {
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur = cur.next;
        }
        if (head.data == key) {
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size() {
        int size = 0;
        Node cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    //打印链表
    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.data + " ");
        }
        System.out.println();
    }
    //清空链表
    public void clear() {
        head = null;
    }
}
